/**
 * Copyright 2010 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package saig.engine.models.enemies;

import java.awt.Graphics;

import saig.engine.Context;
import saig.engine.models.Model;
import saig.engine.models.Sprite;
import saig.engine.models.enums.ModelType;
import saig.engine.models.interfaces.Colorable;
import saig.engine.models.interfaces.Flyable;
import saig.engine.models.interfaces.Damageable;
import saig.engine.resources.ImageLoader;

/**
 * Koopa Troopa enemy model.
 * @author Adam
 * @version $Id$
 * 
 * Created on Aug 11, 2010 at 10:47:55 PM 
 */
public class KoopaTroopa extends Model implements Flyable, Colorable, Damageable {
	/**
	 * The color of this model.
	 * @see saig.engine.models.interfaces.Colorable
	 */
	private int color;

	/**
	 * Used to determine if the KoopaTroopa is dead.
	 */
	private boolean dead = false;

	/**
	 * The current frame for the wing.
	 */
	private int wingFrame;

	/**
	 * The wing frames.
	 */
	private Sprite[] wingFrames;

	/**
	 * Whether or not this model has wings or not.
	 * @see saig.engine.models.interfaces.Flyable
	 */
	private boolean wings;

	/**
	 * @param context
	 */
	public KoopaTroopa(Context context) {
		super(context, ModelType.KOOPA_TROOPA);
		Sprite[] sprites = ImageLoader.getInstance().getSprites("enemies.png", 16, 32);
		frames = new Sprite[3];
		frames[0] = sprites[16];
		frames[1] = sprites[17];
		frames[2] = sprites[18];
		wingFrames = new Sprite[2];
		wingFrames[0] = sprites[112];
		wingFrames[1] = sprites[113];
	}

	/**
	 * Draws the model object to the graphics buffer.
	 * @param g The graphics buffer.
	 * @param cameraX The camera's x position (upper left).
	 * @param cameraY The camera's y position (upper left).
	 */
	@Override
	public void doRender(final Graphics g, final float cameraX, final float cameraY) {
		final Sprite sprite = frames[frame];
		final Sprite wingSprite = wingFrames[wingFrame];
		final int xPos = ((int) (x - cameraX)) - (sprite.getWidth() / 2);
		final int yPos = ((int) (y - cameraY)) - sprite.getHeight() + 1;
		if (direction < 0) {
			g.drawImage(sprite.getImage(), xPos + sprite.getWidth(), yPos,
					-sprite.getWidth(), sprite.getHeight(), null);
			if (wings) {
				g.drawImage(wingSprite.getImage(), xPos + sprite.getWidth() + 10, yPos - 14,
						-sprite.getWidth(), sprite.getHeight(), null);
			}
		} else {
			g.drawImage(sprite.getImage(), xPos, yPos, null);
			if (wings) {
				g.drawImage(wingSprite.getImage(), xPos - 10, yPos - 14, null);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Colorable#getColor()
	 */
	@Override
	public int getColor() {
		return color;
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.Model#getHeight()
	 */
	@Override
	public int getHeight() {
		return 27;
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.Model#getWidth()
	 */
	@Override
	public int getWidth() {
		return 16;
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Flyable#hasWings()
	 */
	@Override
	public boolean hasWings() {
		return wings;
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Damageable#isDead()
	 */
	@Override
	public boolean isDead() {
		return dead;
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.Model#performUpdate()
	 */
	@Override
	public void performUpdate() {
		// TODO Auto-generated method stub
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Colorable#setColor(int)
	 */
	@Override
	public void setColor(int color) {
		this.color = color;
		Sprite[] sprites = ImageLoader.getInstance().getSprites("enemies.png", 16, 32);
		switch (color) {
		case Colorable.BLUE:
			frames[0] = sprites[48];
			frames[1] = sprites[49];
			frames[2] = sprites[50];
			break;
		case Colorable.GREEN:
			frames[0] = sprites[16];
			frames[1] = sprites[17];
			frames[2] = sprites[18];
			break;
		case Colorable.RED:
			frames[0] = sprites[32];
			frames[1] = sprites[33];
			frames[2] = sprites[34];
			break;
		case Colorable.YELLOW:
			frames[0] = sprites[64];
			frames[1] = sprites[65];
			frames[2] = sprites[66];
			break;
			
		}
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Flyable#setWings(boolean)
	 */
	@Override
	public void setWings(boolean wings) {
		this.wings = wings;
		
	}

	/**
	 * {@inheritDoc}
	 * @see saig.engine.models.interfaces.Damageable#takeDamage()
	 */
	@Override
	public void takeDamage() {
		if (wings) {
			wings = false;
		} else {
			dead = true;
		}
	}
}
